package javolution.lang;

import cn.qqtheme.framework.adapter.FileAdapter;
import de.innosystec.unrar.unpack.vm.VMCmdFlags;
import java.util.Random;

/* loaded from: classes2.dex */
public final class MathLib {
    public static final double E = 2.718281828459045d;
    public static final double FOUR_PI = 12.566370614359172d;
    public static final double HALF_PI = 1.5707963267948966d;
    public static final double Infinity = Double.POSITIVE_INFINITY;
    public static final double LOG10 = 2.302585092994046d;
    public static final double LOG2 = 0.6931471805599453d;
    private static final double LOG2_DIV_LOG10 = 0.3010299956639812d;
    static final double Lg1 = 0.6666666666666735d;
    static final double Lg2 = 0.3999999999940942d;
    static final double Lg3 = 0.2857142874366239d;
    static final double Lg4 = 0.22222198432149784d;
    static final double Lg5 = 0.1818357216161805d;
    static final double Lg6 = 0.15313837699209373d;
    static final double Lg7 = 0.14798198605116586d;
    private static final long MASK_32 = 4294967295L;
    private static final long MASK_63 = Long.MAX_VALUE;
    private static final double NORMALIZATION_FACTOR = 4.6566128730773926E-10d;
    public static final double NaN = Double.NaN;
    static final double P1 = 0.16666666666666602d;
    static final double P2 = -0.0027777777777015593d;
    static final double P3 = 6.613756321437934E-5d;
    static final double P4 = -1.6533902205465252E-6d;
    static final double P5 = 4.1381367970572385E-8d;
    public static final double PI = 3.141592653589793d;
    public static final double PI_SQUARE = 9.869604401089358d;
    public static final double SQRT2 = 1.4142135623730951d;
    public static final double TWO_PI = 6.283185307179586d;
    static final double huge = 1.0E300d;
    static final double invln2 = 1.4426950408889634d;
    static final double o_threshold = 709.782712893384d;
    static final double one = 1.0d;
    static final double two54 = 1.8014398509481984E16d;
    static final double twom1000 = 9.332636185032189E-302d;
    static final double u_threshold = -745.1332191019411d;
    static final double zero = 0.0d;
    private static final Random RANDOM = new Random();
    private static final byte[] BIT_LENGTH = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8};
    private static final int[] POW5_INT = {1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125};
    private static double INV_LOG10 = 0.4342944819032518d;
    static final double[] atanhi = {0.4636476090008061d, 0.7853981633974483d, 0.982793723247329d, 1.5707963267948966d};
    static final double[] atanlo = {2.2698777452961687E-17d, 3.061616997868383E-17d, 1.3903311031230998E-17d, 6.123233995736766E-17d};
    static final double[] aT = {0.3333333333333293d, -0.19999999999876483d, 0.14285714272503466d, -0.11111110405462356d, 0.09090887133436507d, -0.0769187620504483d, 0.06661073137387531d, -0.058335701337905735d, 0.049768779946159324d, -0.036531572744216916d, 0.016285820115365782d};
    static final double[] halF = {0.5d, -0.5d};
    static final double ln2_hi = 0.6931471803691238d;
    static final double[] ln2HI = {ln2_hi, -0.6931471803691238d};
    static final double ln2_lo = 1.9082149292705877E-10d;
    static final double[] ln2LO = {ln2_lo, -1.9082149292705877E-10d};

    private MathLib() {
    }

    static double _atan(double d) {
        double d2;
        char c;
        long doubleToLongBits = Double.doubleToLongBits(d);
        int i = (int) (doubleToLongBits >> 32);
        int i2 = (int) doubleToLongBits;
        int i3 = Integer.MAX_VALUE & i;
        if (i3 >= 1141899264) {
            if (i3 > 2146435072 || (i3 == 2146435072 && i2 != 0)) {
                return d + d;
            }
            double[] dArr = atanhi;
            return i > 0 ? dArr[3] + atanlo[3] : (-dArr[3]) - atanlo[3];
        }
        if (i3 >= 1071382528) {
            double abs = abs(d);
            if (i3 < 1072889856) {
                if (i3 < 1072037888) {
                    d2 = ((abs * 2.0d) - one) / (abs + 2.0d);
                    c = 0;
                } else {
                    d2 = (abs - one) / (abs + one);
                    c = 1;
                }
            } else if (i3 < 1073971200) {
                d2 = (abs - 1.5d) / ((abs * 1.5d) + one);
                c = 2;
            } else {
                d2 = (-1.0d) / abs;
                c = 3;
            }
        } else if (i3 < 1042284544 && d + huge > one) {
            return d;
        } else {
            c = 65535;
            d2 = d;
        }
        double d3 = d2 * d2;
        double d4 = d3 * d3;
        double[] dArr2 = aT;
        double d5 = d3 * (dArr2[0] + ((dArr2[2] + ((dArr2[4] + ((dArr2[6] + ((dArr2[8] + (dArr2[10] * d4)) * d4)) * d4)) * d4)) * d4));
        double d6 = d4 * (dArr2[1] + ((dArr2[3] + ((dArr2[5] + ((dArr2[7] + (dArr2[9] * d4)) * d4)) * d4)) * d4));
        if (c < 0) {
            return d2 - ((d5 + d6) * d2);
        }
        double d7 = atanhi[c] - ((((d5 + d6) * d2) - atanlo[c]) - d2);
        return i < 0 ? -d7 : d7;
    }

    static double _ieee754_exp(double d) {
        double d2;
        double d3;
        int i;
        double d4;
        double d5;
        long doubleToLongBits = Double.doubleToLongBits(d);
        int i2 = (int) (doubleToLongBits >> 32);
        int i3 = (int) doubleToLongBits;
        int i4 = (i2 >> 31) & 1;
        int i5 = Integer.MAX_VALUE & i2;
        double d6 = 0.0d;
        if (i5 >= 1082535490) {
            if (i5 >= 2146435072) {
                if ((i3 | (1048575 & i5)) != 0) {
                    return d + d;
                }
                if (i4 == 0) {
                    return d;
                }
                return 0.0d;
            } else if (d > o_threshold) {
                return Double.POSITIVE_INFINITY;
            } else {
                if (d < u_threshold) {
                    return 0.0d;
                }
            }
        }
        int i6 = 0;
        if (i5 > 1071001154) {
            if (i5 < 1072734898) {
                d5 = ln2LO[i4];
                i = (1 - i4) - i4;
                d4 = d - ln2HI[i4];
            } else {
                i = (int) ((invln2 * d) + halF[i4]);
                double d7 = i;
                double d8 = ln2HI[0];
                Double.isNaN(d7);
                double d9 = ln2LO[0];
                Double.isNaN(d7);
                d4 = d - (d8 * d7);
                d5 = d7 * d9;
            }
            d2 = d4 - d5;
            double d10 = d5;
            i6 = i;
            d3 = d4;
            d6 = d10;
        } else if (i5 < 1043333120 && d + huge > one) {
            return d + one;
        } else {
            d2 = d;
            d3 = 0.0d;
        }
        double d11 = d2 * d2;
        double d12 = d2 - (d11 * ((((((((P5 * d11) + P4) * d11) + P3) * d11) + P2) * d11) + P1));
        if (i6 == 0) {
            return one - (((d2 * d12) / (d12 - 2.0d)) - d2);
        }
        long doubleToLongBits2 = Double.doubleToLongBits(one - ((d6 - ((d2 * d12) / (2.0d - d12))) - d3));
        int i7 = (int) (doubleToLongBits2 >> 32);
        if (i6 >= -1021) {
            return Double.longBitsToDouble((doubleToLongBits2 & 4294967295L) | (((i7 + (i6 << 20)) & 4294967295L) << 32));
        }
        return Double.longBitsToDouble((doubleToLongBits2 & 4294967295L) | (((i7 + ((i6 + 1000) << 20)) & 4294967295L) << 32)) * twom1000;
    }

    static double _ieee754_log(double d) {
        int i;
        double d2;
        int i2;
        long doubleToLongBits = Double.doubleToLongBits(d);
        int i3 = (int) (doubleToLongBits >> 32);
        int i4 = (int) doubleToLongBits;
        if (i3 >= 1048576) {
            i = 0;
            d2 = d;
        } else if ((i4 | (Integer.MAX_VALUE & i3)) == 0) {
            return Double.NEGATIVE_INFINITY;
        } else {
            if (i3 < 0) {
                return (d - d) / 0.0d;
            }
            i = -54;
            double d3 = two54 * d;
            i3 = (int) (Double.doubleToLongBits(d3) >> 32);
            d2 = d3;
        }
        if (i3 >= 2146435072) {
            return d2 + d2;
        }
        int i5 = i3 & 1048575;
        int i6 = i + ((i3 >> 20) - 1023) + ((1048576 & (614244 + i5)) >> 20);
        double longBitsToDouble = Double.longBitsToDouble((Double.doubleToLongBits(d2) & 4294967295L) | ((((1072693248 ^ i2) | i5) & 4294967295L) << 32)) - one;
        if (((i5 + 2) & 1048575) < 3) {
            if (longBitsToDouble == 0.0d) {
                if (i6 == 0) {
                    return 0.0d;
                }
                double d4 = i6;
                Double.isNaN(d4);
                double d5 = ln2_hi * d4;
                Double.isNaN(d4);
                return d5 + (d4 * ln2_lo);
            }
            double d6 = longBitsToDouble * longBitsToDouble * (0.5d - (0.3333333333333333d * longBitsToDouble));
            if (i6 == 0) {
                return longBitsToDouble - d6;
            }
            double d7 = i6;
            Double.isNaN(d7);
            double d8 = ln2_hi * d7;
            Double.isNaN(d7);
            return d8 - ((d6 - (d7 * ln2_lo)) - longBitsToDouble);
        }
        double d9 = longBitsToDouble / (2.0d + longBitsToDouble);
        double d10 = i6;
        double d11 = d9 * d9;
        double d12 = d11 * d11;
        int i7 = (i5 - 398458) | (440401 - i5);
        double d13 = (d11 * ((d12 * ((((Lg7 * d12) + Lg5) * d12) + Lg3)) + Lg1)) + (((((Lg6 * d12) + Lg4) * d12) + Lg2) * d12);
        if (i7 <= 0) {
            if (i6 == 0) {
                return longBitsToDouble - (d9 * (longBitsToDouble - d13));
            }
            Double.isNaN(d10);
            double d14 = ln2_hi * d10;
            Double.isNaN(d10);
            return d14 - (((d9 * (longBitsToDouble - d13)) - (d10 * ln2_lo)) - longBitsToDouble);
        }
        double d15 = 0.5d * longBitsToDouble * longBitsToDouble;
        if (i6 == 0) {
            return longBitsToDouble - (d15 - (d9 * (d13 + d15)));
        }
        Double.isNaN(d10);
        double d16 = ln2_hi * d10;
        Double.isNaN(d10);
        return d16 - ((d15 - ((d9 * (d13 + d15)) + (d10 * ln2_lo))) - longBitsToDouble);
    }

    public static double abs(double d) {
        return d < 0.0d ? -d : d;
    }

    public static float abs(float f) {
        return f < 0.0f ? -f : f;
    }

    public static int abs(int i) {
        return i < 0 ? -i : i;
    }

    public static long abs(long j) {
        return j < 0 ? -j : j;
    }

    public static double acos(double d) {
        return 1.5707963267948966d - asin(d);
    }

    public static double asin(double d) {
        if (d < -1.0d || d > one) {
            return Double.NaN;
        }
        if (d == -1.0d) {
            return -1.5707963267948966d;
        }
        if (d == one) {
            return 1.5707963267948966d;
        }
        return atan(d / sqrt(one - (d * d)));
    }

    public static double atan(double d) {
        return _atan(d);
    }

    public static double atan2(double d, double d2) {
        if (abs(d2) <= 1.0E-128d) {
            if (d > 1.0E-128d) {
                return 1.5707963267948966d;
            }
            return d < -1.0E-128d ? 4.71238898038469d : 0.0d;
        }
        double atan = atan(abs(d) / abs(d2));
        if (d2 < 0.0d) {
            atan = 3.141592653589793d - atan;
        }
        return d < 0.0d ? 6.283185307179586d - atan : atan;
    }

    public static int bitLength(int i) {
        if (i < 0) {
            i = -(i + 1);
        }
        return i < 65536 ? i < 256 ? BIT_LENGTH[i] : BIT_LENGTH[i >>> 8] + 8 : i < 16777216 ? BIT_LENGTH[i >>> 16] + VMCmdFlags.VMCF_PROC : BIT_LENGTH[i >>> 24] + 24;
    }

    public static int bitLength(long j) {
        int i = (int) (j >> 32);
        if (i > 0) {
            byte[] bArr = BIT_LENGTH;
            return i < 65536 ? i < 256 ? bArr[i] + VMCmdFlags.VMCF_USEFLAGS : bArr[i >>> 8] + 40 : i < 16777216 ? bArr[i >>> 16] + 48 : bArr[i >>> 24] + 56;
        } else if (i < 0) {
            return bitLength(-(j + 1));
        } else {
            int i2 = (int) j;
            if (i2 < 0) {
                return 32;
            }
            byte[] bArr2 = BIT_LENGTH;
            return i2 < 65536 ? i2 < 256 ? bArr2[i2] : bArr2[i2 >>> 8] + 8 : i2 < 16777216 ? bArr2[i2 >>> 16] + VMCmdFlags.VMCF_PROC : bArr2[i2 >>> 24] + 24;
        }
    }

    public static double ceil(double d) {
        return Math.ceil(d);
    }

    public static double cos(double d) {
        return Math.cos(d);
    }

    public static double cosh(double d) {
        return (exp(d) + exp(-d)) * 0.5d;
    }

    public static int digitLength(int i) {
        if (i < 0) {
            i = -(i + 1);
        }
        if (i >= 100000) {
            if (i < 10000000) {
                return i >= 1000000 ? 7 : 6;
            } else if (i >= 1000000000) {
                return 10;
            } else {
                return i >= 100000000 ? 9 : 8;
            }
        } else if (i >= 100) {
            if (i >= 10000) {
                return 5;
            }
            return i >= 1000 ? 4 : 3;
        } else if (i >= 10) {
            return 2;
        } else {
            return i >= 1 ? 1 : 0;
        }
    }

    public static int digitLength(long j) {
        if (j < 0) {
            j = -(j + 1);
        }
        if (j > 2147483647L) {
            if (j >= 100000000000000L) {
                if (j < 10000000000000000L) {
                    return j >= 1000000000000000L ? 16 : 15;
                } else if (j >= 1000000000000000000L) {
                    return 19;
                } else {
                    return j >= 100000000000000000L ? 18 : 17;
                }
            } else if (j < 100000000000L) {
                return j >= 10000000000L ? 11 : 10;
            } else if (j >= 10000000000000L) {
                return 14;
            } else {
                return j >= 1000000000000L ? 13 : 12;
            }
        }
        int i = (int) j;
        if (i >= 100000) {
            if (i < 10000000) {
                return i >= 1000000 ? 7 : 6;
            } else if (i >= 1000000000) {
                return 10;
            } else {
                return i >= 100000000 ? 9 : 8;
            }
        } else if (i >= 100) {
            if (i >= 10000) {
                return 5;
            }
            return i >= 1000 ? 4 : 3;
        } else if (i >= 10) {
            return 2;
        } else {
            return i >= 1 ? 1 : 0;
        }
    }

    public static double exp(double d) {
        return _ieee754_exp(d);
    }

    public static double floor(double d) {
        return Math.floor(d);
    }

    public static int floorLog10(double d) {
        double floorLog2 = floorLog2(d);
        Double.isNaN(floorLog2);
        int i = (int) (floorLog2 * LOG2_DIV_LOG10);
        double doublePow10 = toDoublePow10(1L, i);
        return (doublePow10 > d || 10.0d * doublePow10 <= d) ? doublePow10 > d ? i - 1 : i + 1 : i;
    }

    public static int floorLog2(double d) {
        if (d > 0.0d) {
            int doubleToLongBits = ((int) (Double.doubleToLongBits(d) >> 52)) & 2047;
            if (doubleToLongBits != 2047) {
                return doubleToLongBits == 0 ? floorLog2(d * two54) - 54 : doubleToLongBits - 1023;
            }
            throw new ArithmeticException("Infinity or NaN");
        }
        throw new ArithmeticException("Negative number or zero");
    }

    public static double log(double d) {
        return _ieee754_log(d);
    }

    public static double log10(double d) {
        return log(d) * INV_LOG10;
    }

    public static double max(double d, double d2) {
        return d >= d2 ? d : d2;
    }

    public static float max(float f, float f2) {
        return f >= f2 ? f : f2;
    }

    public static int max(int i, int i2) {
        return i >= i2 ? i : i2;
    }

    public static long max(long j, long j2) {
        return j >= j2 ? j : j2;
    }

    public static double min(double d, double d2) {
        return d < d2 ? d : d2;
    }

    public static float min(float f, float f2) {
        return f < f2 ? f : f2;
    }

    public static int min(int i, int i2) {
        return i < i2 ? i : i2;
    }

    public static long min(long j, long j2) {
        return j < j2 ? j : j2;
    }

    public static double pow(double d, double d2) {
        return Math.pow(d, d2);
    }

    public static double random() {
        double random = random(0, Integer.MAX_VALUE);
        Double.isNaN(random);
        return random * NORMALIZATION_FACTOR;
    }

    public static double random(double d, double d2) {
        double nextDouble = RANDOM.nextDouble();
        return ((d2 * nextDouble) + d) - (nextDouble * d);
    }

    public static float random(float f, float f2) {
        return (float) random(f, f2);
    }

    public static int random(int i, int i2) {
        int nextInt = RANDOM.nextInt();
        if (nextInt < i || nextInt > i2) {
            int i3 = nextInt - Integer.MIN_VALUE;
            if (i3 < i || i3 > i2) {
                int i4 = (i2 + 1) - i;
                if (i4 > 0) {
                    return abs(i3 % i4) + i;
                }
                throw new Error("Interval [" + i + FileAdapter.DIR_PARENT + i2 + "] error");
            }
            return i3;
        }
        return nextInt;
    }

    public static long random(long j, long j2) {
        long nextLong = RANDOM.nextLong();
        if (nextLong < j || nextLong > j2) {
            long j3 = nextLong - Long.MIN_VALUE;
            if (j3 < j || j3 > j2) {
                long j4 = (j2 + 1) - j;
                if (j4 > 0) {
                    return abs(j3 % j4) + j;
                }
                throw new Error("Interval error");
            }
            return j3;
        }
        return nextLong;
    }

    public static double rem(double d, double d2) {
        double d3 = d / d2;
        if (abs(d3) <= 9.223372036854776E18d) {
            double round = round(d3);
            Double.isNaN(round);
            return d - (round * d2);
        }
        return Double.NaN;
    }

    public static int round(float f) {
        return (int) floor(f + 0.5f);
    }

    public static long round(double d) {
        return (long) floor(d + 0.5d);
    }

    public static double sin(double d) {
        return Math.sin(d);
    }

    public static double sinh(double d) {
        return (exp(d) - exp(-d)) * 0.5d;
    }

    public static double sqrt(double d) {
        return Math.sqrt(d);
    }

    public static double tan(double d) {
        return Math.tan(d);
    }

    public static double tanh(double d) {
        double d2 = d * 2.0d;
        return (exp(d2) - one) / (exp(d2) + one);
    }

    public static double toDegrees(double d) {
        return d * 57.29577951308232d;
    }

    public static double toDoublePow10(long j, int i) {
        long j2;
        long j3;
        long j4 = j;
        int i2 = i;
        long j5 = 0;
        if (j4 == 0) {
            return 0.0d;
        }
        int i3 = 1;
        if (j4 == Long.MIN_VALUE) {
            return toDoublePow10(-922337203685477580L, i2 + 1);
        }
        if (j4 < 0) {
            return -toDoublePow10(-j4, i2);
        }
        int i4 = 0;
        if (i2 >= 0) {
            if (i2 > 308) {
                return Double.POSITIVE_INFINITY;
            }
            long j6 = j4 & 4294967295L;
            long j7 = j4 >>> 32;
            long j8 = 0;
            while (i2 != 0) {
                int[] iArr = POW5_INT;
                int length = i2 >= iArr.length ? iArr.length - i3 : i2;
                int i5 = POW5_INT[length];
                if (((int) j5) != 0) {
                    j5 *= i5;
                }
                if (((int) j8) != 0) {
                    j8 *= i5;
                }
                long j9 = i5;
                long j10 = j8 + (j5 >>> 32);
                j5 &= 4294967295L;
                long j11 = (j6 * j9) + (j10 >>> 32);
                j8 = j10 & 4294967295L;
                j7 = (j7 * j9) + (j11 >>> 32);
                j6 = j11 & 4294967295L;
                i4 += length;
                i2 -= length;
                long j12 = j7 >>> 32;
                if (j12 != 0) {
                    i4 += 32;
                    j5 = j8;
                    j8 = j6;
                    j6 = j7 & 4294967295L;
                    j7 = j12;
                }
                i3 = 1;
            }
            int bitLength = 31 - bitLength(j7);
            int i6 = i4 - bitLength;
            if (bitLength < 0) {
                j2 = j7 << 31;
                j3 = j6 >>> 1;
            } else {
                j2 = ((j7 << 32) | j6) << bitLength;
                j3 = j8 >>> (32 - bitLength);
            }
            return toDoublePow2(j2 | j3, i6);
        }
        long j13 = 0;
        if (i2 < -344) {
            return 0.0d;
        }
        while (true) {
            int bitLength2 = 63 - bitLength(j4);
            long j14 = (j4 << bitLength2) | (j13 >>> (63 - bitLength2));
            long j15 = (j13 << bitLength2) & Long.MAX_VALUE;
            int i7 = i4 - bitLength2;
            if (i2 == 0) {
                return toDoublePow2(j14, i7);
            }
            int i8 = -i2;
            int[] iArr2 = POW5_INT;
            if (i8 >= iArr2.length) {
                i8 = iArr2.length - 1;
            }
            long j16 = j14 >>> 32;
            long j17 = POW5_INT[i8];
            long j18 = j16 / j17;
            long j19 = (j14 & 4294967295L) | ((j16 - (j18 * j17)) << 32);
            long j20 = j19 / j17;
            long j21 = ((j19 - (j20 * j17)) << 31) | (j15 >>> 32);
            long j22 = j21 / j17;
            j13 = (j22 << 32) | ((((j21 - (j22 * j17)) << 32) | (j15 & 4294967295L)) / j17);
            i2 += i8;
            i4 = i7 - i8;
            j4 = j20 | (j18 << 32);
        }
    }

    public static double toDoublePow2(long j, int i) {
        if (j == 0) {
            return 0.0d;
        }
        if (j == Long.MIN_VALUE) {
            return toDoublePow2(-4611686018427387904L, i + 1);
        }
        if (j < 0) {
            return -toDoublePow2(-j, i);
        }
        int bitLength = bitLength(j) - 53;
        long j2 = i + 1075 + bitLength;
        if (j2 >= 2047) {
            return Double.POSITIVE_INFINITY;
        }
        if (j2 <= 0) {
            if (j2 <= -54) {
                return 0.0d;
            }
            return toDoublePow2(j, i + 54) / two54;
        }
        long j3 = bitLength > 0 ? (j >> bitLength) + ((j >> (bitLength - 1)) & 1) : j << (-bitLength);
        if ((j3 >> 52) != 1) {
            j2++;
            if (j2 >= 2047) {
                return Double.POSITIVE_INFINITY;
            }
        }
        return Double.longBitsToDouble((4503599627370495L & j3) | (j2 << 52));
    }

    public static long toLongPow10(double d, int i) {
        long j;
        int i2;
        long doubleToLongBits = Double.doubleToLongBits(d);
        long j2 = 0;
        boolean z = (doubleToLongBits >> 63) != 0;
        int i3 = ((int) (doubleToLongBits >> 52)) & 2047;
        long j3 = doubleToLongBits & 4503599627370495L;
        if (i3 != 2047) {
            if (i3 == 0) {
                if (j3 == 0) {
                    return 0L;
                }
                return toLongPow10(1.0E16d * d, i - 16);
            }
            long j4 = j3 | 4503599627370496L;
            int i4 = (i3 - 1023) - 52;
            if (i >= 0) {
                long j5 = j4 & 4294967295L;
                long j6 = j4 >>> 32;
                long j7 = 0;
                int i5 = i;
                while (i5 != 0) {
                    int[] iArr = POW5_INT;
                    int length = i5 >= iArr.length ? iArr.length - 1 : i5;
                    int i6 = POW5_INT[length];
                    int i7 = i4;
                    if (((int) j2) != 0) {
                        j2 *= i6;
                    }
                    if (((int) j7) != 0) {
                        j7 *= i6;
                    }
                    long j8 = i6;
                    long j9 = j7 + (j2 >>> 32);
                    j2 &= 4294967295L;
                    long j10 = (j5 * j8) + (j9 >>> 32);
                    j7 = j9 & 4294967295L;
                    j6 = (j6 * j8) + (j10 >>> 32);
                    j5 = j10 & 4294967295L;
                    int i8 = i7 + length;
                    i5 -= length;
                    long j11 = j6 >>> 32;
                    if (j11 != 0) {
                        i4 = i8 + 32;
                        j2 = j7;
                        j7 = j5;
                        j5 = j6 & 4294967295L;
                        j6 = j11;
                    } else {
                        i4 = i8;
                    }
                }
                int bitLength = 31 - bitLength(j6);
                i2 = i4 - bitLength;
                j = bitLength < 0 ? (j6 << 31) | (j5 >>> 1) : (((j6 << 32) | j5) << bitLength) | (j7 >>> (32 - bitLength));
            } else {
                int i9 = i;
                long j12 = 0;
                while (true) {
                    int bitLength2 = 63 - bitLength(j4);
                    j = (j4 << bitLength2) | (j12 >>> (63 - bitLength2));
                    long j13 = (j12 << bitLength2) & Long.MAX_VALUE;
                    i2 = i4 - bitLength2;
                    if (i9 == 0) {
                        break;
                    }
                    int i10 = -i9;
                    int[] iArr2 = POW5_INT;
                    if (i10 >= iArr2.length) {
                        i10 = iArr2.length - 1;
                    }
                    long j14 = j >>> 32;
                    long j15 = POW5_INT[i10];
                    long j16 = j14 / j15;
                    long j17 = (j & 4294967295L) | ((j14 - (j16 * j15)) << 32);
                    long j18 = j17 / j15;
                    long j19 = (j16 << 32) | j18;
                    long j20 = ((j17 - (j18 * j15)) << 31) | (j13 >>> 32);
                    long j21 = j20 / j15;
                    j12 = (j21 << 32) | ((((j20 - (j21 * j15)) << 32) | (j13 & 4294967295L)) / j15);
                    i9 += i10;
                    i4 = i2 - i10;
                    j4 = j19;
                    z = z;
                }
            }
            if (i2 <= 0) {
                if (i2 < -63) {
                    return 0L;
                }
                long j22 = (j >> (-i2)) + ((j >> (-(i2 + 1))) & 1);
                return z ? -j22 : j22;
            }
            throw new ArithmeticException("Overflow");
        }
        throw new ArithmeticException("Cannot convert to long (Infinity or NaN)");
    }

    public static long toLongPow2(double d, int i) {
        long doubleToLongBits = Double.doubleToLongBits(d);
        boolean z = (doubleToLongBits >> 63) != 0;
        int i2 = ((int) (doubleToLongBits >> 52)) & 2047;
        long j = doubleToLongBits & 4503599627370495L;
        if (i2 != 2047) {
            if (i2 == 0) {
                if (j == 0) {
                    return 0L;
                }
                return toLongPow2(d * two54, i - 54);
            }
            long j2 = 4503599627370496L | j;
            long j3 = ((i2 - 1023) - 52) + i;
            if (j3 <= -64) {
                return 0L;
            }
            if (j3 < 11) {
                long j4 = j3 >= 0 ? j2 << ((int) j3) : ((j2 >> ((int) (-(j3 + 1)))) & 1) + (j2 >> ((int) (-j3)));
                return z ? -j4 : j4;
            }
            throw new ArithmeticException("Cannot convert to long (overflow)");
        }
        throw new ArithmeticException("Cannot convert to long (Infinity or NaN)");
    }

    public static double toRadians(double d) {
        return d * 0.017453292519943295d;
    }
}
